package com.xiaoyu.izf.service;

import com.xiaoyu.izf.model.AppSendMsgCode;
import com.xiaoyu.izf.repository.AppSendMsgCodeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

@Service
public class AppSendMsgCodeService {
    @Autowired
    private AppSendMsgCodeDao appSendMsgCodeDao;

    public AppSendMsgCode codeSave(String cid, String checkCode){
        AppSendMsgCode appSendMsgCode = appSendMsgCodeDao.findByAppCid(cid);
        if(appSendMsgCode==null){
            appSendMsgCode = new AppSendMsgCode();
            appSendMsgCode.setAppCid(cid);
        }
        appSendMsgCode.setCheckCode(checkCode);
        appSendMsgCode.setMsgCode("");
        appSendMsgCode.setInsertTime(new Timestamp(System.currentTimeMillis()));
        appSendMsgCode.setSendTime(new Timestamp(System.currentTimeMillis()));
        return appSendMsgCodeDao.save(appSendMsgCode);
    }

    public AppSendMsgCode setMsgCode(String cid, String msgCode){
        AppSendMsgCode appSendMsgCode = getOne(cid);
        appSendMsgCode.setMsgCode(msgCode);
        appSendMsgCode.setCheckCode("");//设置为已过期
        appSendMsgCode.setSendTime(new Timestamp(System.currentTimeMillis()));
        return appSendMsgCodeDao.save(appSendMsgCode);
    }

    public AppSendMsgCode clrMsgCode(String cid){
        AppSendMsgCode appSendMsgCode = getOne(cid);
        appSendMsgCode.setMsgCode("");
        return appSendMsgCodeDao.save(appSendMsgCode);
    }

    public AppSendMsgCode getOne(String cid){
        return appSendMsgCodeDao.getOne(cid);
    }
}
